/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bmf.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

/**
 *
 * @author Tony_
 */
@Path("groups")
@Stateless
public class gResource {

    @Context
    @EJB
    private nManagerLocal nManagerLocal;
    @Context
    UriInfo uriInfo;
    @Context
    Request request;
    
    @GET
    @Produces({MediaType.TEXT_XML})
    public List<Group> getIds() {
      List<Group> l=nManagerLocal.getGroups();
      
      return l;
    }
    /*@PUT
    @Consumes(MediaType.APPLICATION_XML)
	public Response putUser(JAXBElement<Group> group) {
		Group g = group.getValue();
		return putAndGetResponse(g);
	}*/
    @POST
	@Produces(MediaType.TEXT_HTML)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void newGroup(
			@FormParam("id") String id,
			@FormParam("name") String name,
			
			@Context HttpServletResponse servletResponse
	) throws IOException {
		com.bmf.rest.Group r = new com.bmf.rest.Group();
		r.setId(id);
                r.setName(name);
                
		nManagerLocal.put(r);
		servletResponse.sendRedirect("requests.faces");
	}
    
    private Response putAndGetResponse(Group g) {
	Response res;
		if(nManagerLocal.getGroups().contains(g)) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		nManagerLocal.put(g);
	return res;
	}
        /*@Path("{id}")
	public List<Node> getNodes(@PathParam("id") String id) {
		return nManagerLocal.getNodes(id);
	}*/
        @Path("{id}")
	public List<Node> getNodesN(@PathParam("id") String id) {
		return nManagerLocal.getNodesN(id);
	}
        /*@GET
	@Produces( { MediaType.TEXT_XML })
	public List<Node> getNodesBrowser() {
		Node test=new Node();
                test.setGroup(new Group("1","default"));
                test.setId("9");
                test.setSensorBoardName("Light");
                Node test1=new Node();
                test.setGroup(new Group("1","default"));
                test.setId("8");
                test.setSensorBoardName("Temperature");
                List<Node> nodes = new ArrayList<Node>();
                nodes.add(test);
                nodes.add(test1);
               
                return nodes;
	}*/

}